home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / Terminal 2.2 / Project / Sources / CancelDialog.c < prev    next >
Text File  |  1992-01-17  |  5KB  |  220 lines

  1. /*
  2.     Terminal 2.2
  3.     "CancelDialog.c"
  4. */
  5.  
  6. #ifdef THINK_C
  7. #include "MacHeaders"
  8. #endif
  9. #ifdef applec
  10. #pragma load ":(Objects):MacHeadersMPW"
  11. #pragma segment Main2
  12. #endif
  13.  
  14. #include "CancelDialog.h"
  15. #include "Text.h"
  16. #include "Main.h"
  17. #include "Document.h"
  18. #include "Strings.h"
  19. #include "Utilities.h"
  20. #include "FormatStr.h"
  21.  
  22. #define PROGRESS_CANCEL    1        /* Cancel button */
  23. #define PROGRESS_ACTION    2        /* Action text */
  24. #define PROGRESS_NAME    3        /* File name */
  25. #define PROGRESS_INFO    4        /* Additional info */
  26. #define PROGRESS_TIME    5        /* Estimated time left */
  27. #define PROGRESS_MARK    6        /* Current file mark */
  28. #define PROGRESS_MAX    7        /* Maximum number of bytes */
  29. #define PROGRESS_USER    8        /* Progress indicator */
  30. #define PROGRESS_BLOCK    9        /* Current block number */
  31. #define PROGRESS_TEXT    10        /* Error message */
  32. #define PROGRESS_ERR    11        /* Error count */
  33.  
  34. static DialogPtr Dialog = 0;    /* Current dialog */
  35. static Rect Box;                /* Progress indicator rectangle */
  36. static short Progress;            /* Current progress value (pixels) */
  37.  
  38. /* ----- Convert seconds to minutes ------------------------------------ */
  39.  
  40. Byte *SecondsToString(register long seconds, register Byte *num)
  41. {
  42.     return FormatStr(num, (Byte *)"\p%l'%02l\"",
  43.         seconds / 60, seconds % 60);
  44. }
  45.  
  46. /* ----- Display statistics message ------------------------------------ */
  47.  
  48. void Statistics(
  49.     register long bytes,        /* Number of bytes transferred */
  50.     register long seconds,        /* Time for transfer in seconds */
  51.     register short error)        /* Error code */
  52. {
  53.     register Byte s[256];
  54.     Byte sec[256];
  55.  
  56.     SysBeep(1);
  57.     MakeMessage(TerminalWindow,
  58.         FormatStr(s, MyString(STR_M, M_TRANSFER),
  59.             bytes, SecondsToString(seconds, sec),
  60.             seconds ? bytes/seconds : bytes, error));
  61. }
  62.  
  63. /* ----- Remove cancel/progress dialog window -------------------------- */
  64.  
  65. void RemoveCancelDialog(void)
  66. {
  67.     Point position;
  68.  
  69.     if (Dialog) {
  70.         position = topLeft(((GrafPtr)Dialog)->portRect);
  71.         SetPort(Dialog);
  72.         LocalToGlobal(&position);
  73.         if (position.h != Settings.progressWindow.h ||
  74.                 position.v != Settings.progressWindow.v ) {
  75.             Settings.progressWindow = position;
  76.             Settings.dirty = TRUE;
  77.         }
  78.         DisposDialog(Dialog);
  79.         Dialog = 0;
  80.         RedrawDocument();
  81.         DisableItem(GetMenu(EDIT), SHOWPW);
  82.     }
  83. }
  84.  
  85. /* ----- Check if canceled --------------------------------------------- */
  86.  
  87. Boolean CheckCancel(void)
  88. {
  89.     EventRecord event;
  90.     DialogPtr dialog;
  91.     short button;
  92.  
  93.     do {
  94.         if (WNE)
  95.             WaitNextEvent(everyEvent, &event, 0, 0);
  96.         else {
  97.             SystemTask();
  98.             GetNextEvent(everyEvent, &event);
  99.         }
  100.         if (IsDialogEvent(&event) &&
  101.                 DialogSelect(&event, &dialog, &button) &&
  102.                 button == PROGRESS_CANCEL) {
  103.             Transfer = 0;
  104.             return TRUE;    /* Cancel by button */
  105.         }
  106.         DoEvent(&event);
  107.         if (!Transfer)
  108.             return TRUE;    /* Cancel by menu command */
  109.     } while (event.what != nullEvent);
  110.     return FALSE;            /* Not yet canceled */
  111. }
  112.  
  113. /* ----- Draw progress indicator (user item) --------------------------- */
  114.  
  115. static pascal void DrawProgress(
  116.     register WindowPtr window,
  117.     register short itemNo)
  118. {
  119. #pragma unused(window, itemNo)
  120.     Rect box;
  121.     register short i;
  122.  
  123.     box = Box;
  124.     if ((i = box.left + Progress) > box.right)
  125.         i = box.right;
  126.     box.right = i;
  127.     PaintRect(&box);
  128.     box = Box;
  129.     box.left = i;
  130.     EraseRect(&box);
  131.     FrameRect(&Box);
  132. }
  133.  
  134. /* ----- Set file name in progress dialog ------------------------------ */
  135.  
  136. void NameProgress(Byte *name)
  137. {
  138.     SetEText(Dialog, PROGRESS_NAME, name);
  139. }
  140.  
  141. /* ----- Set info text in progress dialog ------------------------------ */
  142.  
  143. void InfoProgress(Byte *info)
  144. {
  145.     SetEText(Dialog, PROGRESS_INFO, info);
  146. }
  147.  
  148. /* ----- Setup and draw progress dialog window ------------------------- */
  149.  
  150. void DrawProgressDialog(
  151.     register short prompt,
  152.     register Byte *name)
  153. {
  154.     register DialogTHndl h;
  155.     register Rect *p;
  156.     short type;
  157.     Handle item;
  158.  
  159.     MakeMessage(TerminalWindow, MyString(STR_M, M_PROGRESS));
  160.     Progress = 0;
  161.     if (h = (DialogTHndl)GetResource('DLOG', DLOG_PROGRESS)) {
  162.         p = &(**h).boundsRect;
  163.         OffsetRect(p, Settings.progressWindow.h - p->left,
  164.             Settings.progressWindow.v - p->top);
  165.     }
  166.     if (Dialog = GetNewDialog(DLOG_PROGRESS, 0L, (WindowPtr)-1L)) {
  167.         SetEText(Dialog, PROGRESS_ACTION, MyString(STR_P, prompt));
  168.         SetEText(Dialog, PROGRESS_NAME, name);
  169.         GetDItem(Dialog, PROGRESS_USER, &type, &item, &Box);
  170.         SetDItem(Dialog, PROGRESS_USER, userItem, (Handle)DrawProgress,
  171.             &Box);
  172.         SetEText(Dialog, PROGRESS_INFO, EmptyStr);
  173.         DrawDialog(Dialog);
  174.         EnableItem(GetMenu(EDIT), SHOWPW);
  175.     }
  176. }
  177.  
  178. /* ----- Update of progress dialog ------------------------------------- */
  179.  
  180. void UpdateProgress(
  181.     register long progress,
  182.     register long maximum,
  183.     long time,
  184.     long block,
  185.     long error,
  186.     Byte *message)
  187. {
  188.     Byte num[10];
  189.  
  190.     if (Dialog) {
  191.         SetPort(Dialog);
  192.  
  193.         if (maximum)
  194.             Progress = ((Box.right - Box.left) * progress) / maximum;
  195.         DrawProgress(Dialog, PROGRESS_USER);
  196.  
  197.         NumToString(progress, num);
  198.         SetEText(Dialog, PROGRESS_MARK, num);
  199.         NumToString(maximum, num);
  200.         SetEText(Dialog, PROGRESS_MAX, num);
  201.  
  202.         SecondsToString(time, num);
  203.         SetEText(Dialog, PROGRESS_TIME, num);
  204.  
  205.         NumToString(block, num);
  206.         SetEText(Dialog, PROGRESS_BLOCK, num);
  207.         NumToString(error, num);
  208.         SetEText(Dialog, PROGRESS_ERR, num);
  209.         SetEText(Dialog, PROGRESS_TEXT, message);
  210.     }
  211. }
  212.  
  213. /* ----- Bring progress window to front -------------------------------- */
  214.  
  215. void SelectCancelDialog(void)
  216. {
  217.     if (Dialog)
  218.         SelectWindow(Dialog);
  219. }
  220.